package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.event.EventPageChange;
import ru.rchervot.sanasar.client.event.HandlerPageChange;
import ru.rchervot.sanasar.client.model.SearchResultsItem;

import com.google.gwt.event.shared.HandlerManager;

public abstract class AbstractRecordSummaryPresenter implements
		Presenter {
	
	public interface Display extends Presenter.Display {
		public void updateDisplay(SearchResultsItem itemDescription);
	}
	
	protected final HandlerManager eventBus;
	protected final Display display;
	
	protected abstract void renderItemDescription(SearchResultsItem itemDescription);

	protected AbstractRecordSummaryPresenter(Display display, 
			HandlerManager eventBus) {
		this.eventBus = eventBus;
		this.display = display;
		
		listenForPageChange();
	}
	
	@Override
	public void bindDisplay() {
		display.setPresenter(this);
	}
	
	private void listenForPageChange() {
		eventBus.addHandler(EventPageChange.getType(),
				new HandlerPageChange<SearchResultsItem>() {
			@Override
			public void onPageChange(EventPageChange<SearchResultsItem> event) {
				if (event.isStartPage()) {
					// view would be set invisible by parent view
				} else {
					if (event.getPage().isEmpty()) {
					// view would be set invisible by parent view
					} else {
						renderItemDescription(event.getPage().getCurrentItem());			
					}
				}
			}
		});
	}
	
}
